package com.lebo.portal.aspect;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.lebo.portal.view.PageInfo;
import com.lebo.portal.view.PageResult;

@Component
@Aspect
public class PagingAdvice {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * Modify paging index after searching in database.
     * @param  pageQuery  object returned by service paging method
     * @return void
     * @see
     */
    @AfterReturning(pointcut = "within(com.lebo.portal.service.*) && execution(* paging*(..))", returning = "pageResult")
    public void modifyPageIndex(PageResult<? extends Object> pageResult) {
         logger.info("paging advice...");
         PageInfo page = pageResult.getPageInfo();
         int remainder = (int) (page.getTotalItem() % page.getPageSize());
         if(0 == remainder) {
             page.setTotalPage((int) (page.getTotalItem() / page.getPageSize()));
         } else {
             page.setTotalPage((int) (page.getTotalItem() / page.getPageSize()) + 1);
         }
    }
}
